package com.gongpingjialoan.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.BuyCarVipActivity;
import com.gongpingjialoan.activity.car.IntentFragment;
import com.gongpingjialoan.activity.category.CategoryActivity;
import com.gongpingjialoan.bean.car.BrandBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/23.
 */
public class AddBrandView extends LinearLayout implements View.OnClickListener {

    private static final int REQUEST_BRAND = 0x1010;

    private List<BrandBean> mBrandBeans;

    private TextView addBrandTextView, noIntentTextView;

    private View noBrandView;

    private LinearLayout addBrandLinearLayout;

    private IntentFragment mIntentFragment;

    private LayoutInflater mLayoutInflater;

    private Activity mActivity;

    public int maxCount = 3;


    public AddBrandView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLayoutInflater = LayoutInflater.from(getContext());
        mActivity = (Activity) context;
        mBrandBeans = new ArrayList<>();
        mLayoutInflater.inflate(R.layout.add_brand_layout, this, true);
        noBrandView = findViewById(R.id.no_car_main);
        addBrandTextView = (TextView) findViewById(R.id.add);
        noIntentTextView = (TextView) findViewById(R.id.nointent);
        addBrandLinearLayout = (LinearLayout) findViewById(R.id.brand_main);
        addBrandTextView.setOnClickListener(this);
        noIntentTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == addBrandTextView) {
            Intent intent = new Intent();
            intent.setClass(mIntentFragment.getActivity(), CategoryActivity.class);
            intent.putExtra("isFromFilter", true);
            intent.putExtra("isShowAllSeries", false);
            intent.putExtra("needModelDetailFragment", false);
            mIntentFragment.startActivityForResult(intent, REQUEST_BRAND);
        } else if (v == noIntentTextView) {
            mIntentFragment.mBuyCarVipActivity.goNoIntent();
        }
    }


    public void setIntentFragment(IntentFragment intentFragment) {
        mIntentFragment = intentFragment;
    }

    public void addBrand(BrandBean brandBean) {
        mBrandBeans.add(brandBean);
        addIsVisviable();
        addBrandView();

    }

    public void setMaxCount(int count) {
        maxCount = count;
    }

    public String getBrandModels() {
        StringBuilder sb = new StringBuilder();
        int size = mBrandBeans.size();
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                sb.append(mBrandBeans.get(i).brand_slug + ",")
                        .append(mBrandBeans.get(i).model_slug);
            } else {
                sb.append(mBrandBeans.get(i).brand_slug + ",")
                        .append(mBrandBeans.get(i).model_slug + ";");
            }
        }
        return sb.toString();
    }


    public String getBrandModelsZH() {
        StringBuilder sb = new StringBuilder();
        int size = mBrandBeans.size();
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                sb.append(mBrandBeans.get(i).brand + ",")
                        .append(mBrandBeans.get(i).model);
            } else {
                sb.append(mBrandBeans.get(i).brand + ",")
                        .append(mBrandBeans.get(i).model + ";");
            }
        }
        return sb.toString();
    }

    public boolean isHaveBrandModels() {
        if (mBrandBeans.size() > 0) {
            return true;
        } else {
            return false;
        }
    }


    private void addBrandView() {
        addBrandLinearLayout.removeAllViews();
        int size = mBrandBeans.size();
        for (int i = 0; i < size; i++) {
            final View view = mLayoutInflater.inflate(R.layout.brand_item, null);
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView delete = (TextView) view.findViewById(R.id.delete);
            name.setText(mBrandBeans.get(i).model);
            delete.setTag(mBrandBeans.get(i));
            delete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBrandBeans.remove(v.getTag());
                    addBrandLinearLayout.removeView(view);
                    if (mActivity instanceof BuyCarVipActivity) {
                        BuyCarVipActivity buyCarVipActivity = (BuyCarVipActivity) mActivity;
                        buyCarVipActivity.mIntentFragment.setSureButton();
                    }
                    addIsVisviable();
                }
            });
            addBrandLinearLayout.addView(view);
        }

    }


    private void addIsVisviable() {
        if (mBrandBeans.size() > 0 && mBrandBeans.size() < maxCount) {
            noBrandView.setVisibility(View.VISIBLE);
            addBrandTextView.setText("继续添加车型");
            mIntentFragment.mMaterialOneSlider.haveBrand = true;
        } else if (mBrandBeans.size() == 0) {
            noBrandView.setVisibility(View.VISIBLE);
            addBrandTextView.setText("添加车型");
            mIntentFragment.mMaterialOneSlider.haveBrand = false;
        } else {
            noBrandView.setVisibility(View.GONE);
            mIntentFragment.mMaterialOneSlider.haveBrand = true;
        }

    }


    public List<BrandBean> getBrandBeans() {
        return mBrandBeans;
    }

}
